package com.yanghang.service.impl;

import com.yanghang.dao.AccountDao;
import com.yanghang.dao.RecordDao;
import com.yanghang.entity.Account;
import com.yanghang.entity.Record;
import com.yanghang.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class AccountServiceImpl implements AccountService {
    @Autowired
    private RecordDao rd;
    @Autowired
    private AccountDao ad;
    @Override
    @Transactional(readOnly = true)
    public Account login(String cardno, String password) {
       return ad.selecAccountByCardNoAndPassword(cardno, password);
    }

    @Override
    public void transforCount(String sendCardNo, String receiveCardNo, Double price) {
        Account sendCount = ad.selectAccountByCardNo(sendCardNo);
        Account receiveCount = ad.selectAccountByCardNo(receiveCardNo);
        System.out.println("----------修改前的账户-----------");
        System.out.println("转款账户:"+sendCount);
        System.out.println("收款账户:"+receiveCount);
        System.out.println("-------------修改后的数据-------------");
        sendCount.setBalance(sendCount.getBalance()-price);
        receiveCount.setBalance(receiveCount.getBalance()+price);
        System.out.println("转款账户:"+sendCount);
        System.out.println("收款账户"+receiveCount);
        System.out.println("------------交易记录数据-------");
        Record r1=new Record(null,sendCardNo,new Date(),-price,sendCount.getBalance(),"转账",null);
        Record r2=new Record(null,receiveCardNo,new Date(),price,receiveCount.getBalance(),"收款" +"",null);
        System.out.println("转款记录:"+r1);
        System.out.println("收款记录:"+r2);
        ad.updateById(sendCount);
        rd.insert(r1);
        ad.updateById(receiveCount);
        rd.insert(r2);


    }

    @Override
    public Account selectAccountByCardNo(String cardno) {
        return ad.selectAccountByCardNo(cardno);
    }
}
